package com.zf.yichat.utils.response;

import com.zf.yichat.utils.common.FsConst;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by fengsong on 2018/7/28.
 */
public class FsResponseGen {
    private static Logger logger = LoggerFactory.getLogger(FsResponseGen.class);

    private static FsResponse success = new FsResponse("操作成功", FsConst.ResponseCode.SUCCESS, null, true);
    private static FsResponse fail = new FsResponse("操作失败", FsConst.ResponseCode.FAIL, null, false);

    public static FsResponse success() {
        return success;
    }

    public static FsResponse successMsg(String str) {
        return new FsResponse(str, FsConst.ResponseCode.SUCCESS, null, true);
    }

    public static FsResponse fail(MsgCode msgCode) {
        FsResponse fail = fail();
        fail.setCode(msgCode.getCode());
        fail.setMsg(msgCode.getMsg());
        logger.warn("Gen fail response:{}", msgCode.getMsg());
        return fail;
    }

    public static FsResponse fail() {
        logger.warn("Gen fail response");
        return fail;
    }

    public static FsResponse failMsg(String msg) {
        logger.warn("Gen fail response:{}", msg);
        return new FsResponse(msg, FsConst.ResponseCode.FAIL, null, false);
    }

    public static FsResponse fail(String code, String msg) {
        logger.warn("Gen fail response:{}", msg);
        return new FsResponse(msg, code, null, false);
    }

    public static <T> FsResponse successData(T t) {
        return new FsResponse("操作成功", FsConst.ResponseCode.SUCCESS, t, true);
    }

    public static <T> FsResponse successData(MsgCode msgCode, T t) {
        return new FsResponse(msgCode.getMsg(), msgCode.getCode(), t, true);
    }

    public static <T> FsResponse success(MsgCode msgCode) {
        return new FsResponse(msgCode.getMsg(), msgCode.getCode(), null, true);
    }


    public static FsResponse gen(boolean flag) {
        return flag ? success() : fail();
    }

    public static FsResponse gen(boolean flag, String failMsg) {
        return flag ? success() : failMsg(failMsg);
    }
}
